/*
 * RationsPaceFrame.java
 * Team qq 2011
 */
package com.google.code.timetrail.gui;

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.google.code.timetrail.backend.*;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import javax.swing.SwingConstants;

public class RationsPaceFrame extends JPanel{
    /**
     * 
     */
    private static final long serialVersionUID = -7826090580628698909L;
    private JLabel paceLabel;
    private JLabel rationsLabel;
    private JList rationsList;
    protected ConsumptionRate rations;
    private JList paceList;
    private JButton nextButton2;
    protected Component frame;
    private AbstractButton backButton;
    private Control myGameControl;
    private RationsPaceFrameBackend rationsPaceBackend;
    private JPanel myCD;
    private String myPrevious;

    public RationsPaceFrame(Control gameControl, JPanel cd, String previous){
        this.myGameControl = gameControl;
        this.myCD = cd;
        this.myPrevious = previous;
        rationsPaceBackend = new RationsPaceFrameBackend(this.myGameControl);
        
        paceLabel = new JLabel("Select Pace:");
        
        rationsLabel = new JLabel("Select Rations:");
        
        rationsList = new JList();
        rationsList.setVisibleRowCount(5);
        rationsList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                rationsPaceBackend.rationsListChanged(rationsList.getSelectedIndex());
                
            }
        });
        rationsList.setModel(new AbstractListModel() {
            /**
             * 
             */
            private static final long serialVersionUID = -4548133300722898773L;
            String[] values = rationsPaceBackend.getRationsValues();
            public int getSize() {
                return values.length;
            }
            public Object getElementAt(int index) {
                return values[index];
            }
        });
        
        paceList = new JList();
        paceList.setVisibleRowCount(4);
        paceList.addListSelectionListener(new ListSelectionListener() {
            

            public void valueChanged(ListSelectionEvent arg0) {
                rationsPaceBackend.paceValueChanged(paceList.getSelectedIndex());

            }
        });
        paceList.setModel(new AbstractListModel() {
            /**
             * 
             */
            private static final long serialVersionUID = 6536129015703048805L;
            String[] values = rationsPaceBackend.getPaceValues();//
            public int getSize() {
                return values.length;
            }
            public Object getElementAt(int index) {
                return values[index];
            }
        });
        
        nextButton2 = new JButton(rationsPaceBackend.getNextButtonText());
        nextButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(rationsPaceBackend.canPressNextButton()){
                	if(myPrevious.equals("Startup")){
		                myCD.add(new StoreFrame(myGameControl, myCD), "name_131784086749183700");
		                CardLayout cd = (CardLayout) myCD.getLayout();
		                cd.last(myCD);
                	} else if(myPrevious.equals("TakeMove")){
                		myCD.add(new TakingMoveFrame(myGameControl, myCD, "Rations"), "name_131784086749183705");
		                CardLayout cd = (CardLayout) myCD.getLayout();
		                cd.last(myCD);
                	}
                }
            }
        });
        
        backButton = new JButton(rationsPaceBackend.getBackButtonText());
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.previous(myCD);
            }
        });
        
        JLabel lblNewLabel = new JLabel("Rations and Pace");
        GroupLayout groupLayout = new GroupLayout(this);
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addContainerGap(268, Short.MAX_VALUE)
                            .addComponent(backButton)
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addComponent(nextButton2))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(238)
                            .addComponent(lblNewLabel))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(190)
                            .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                .addGroup(groupLayout.createSequentialGroup()
                                    .addComponent(paceLabel)
                                    .addPreferredGap(ComponentPlacement.UNRELATED)
                                    .addComponent(paceList, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE))
                                .addGroup(groupLayout.createSequentialGroup()
                                    .addComponent(rationsLabel)
                                    .addPreferredGap(ComponentPlacement.UNRELATED)
                                    .addComponent(rationsList, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)))))
                    .addContainerGap())
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.TRAILING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(33)
                    .addComponent(lblNewLabel)
                    .addGap(49)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(paceLabel)
                        .addComponent(paceList, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
                    .addGap(54)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addComponent(rationsList, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                .addComponent(nextButton2)
                                .addComponent(backButton)))
                        .addComponent(rationsLabel))
                    .addContainerGap())
        );
        groupLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {rationsList, paceList});
        groupLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {nextButton2, backButton, lblNewLabel});
        setLayout(groupLayout);
    }
}
